package com.egao.oa.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.DateUtil;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.oa.biz.AssetsBiz;
import com.egao.oa.entity.Assets;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-08-19 16:54:05
 * @version ：1.0
 */
@Repository
public class AssetsBizImpl extends BaseDao implements AssetsBiz {

	@Override
    public JsonResult list(PageResult2<T> page, Assets assets, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<Assets> rm = new BeanPropertyRowMapper<Assets>(Assets.class);
        StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.name");
        sql.append(",a.num,a.unit,a.startDate,a.price");
		sql.append(",a.newPrice,a.type,a.state,a.remark");
		sql.append(",a.create_time,a.create_user,a.update_time,a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from oa_assets a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id ");
        sql.append(" where a.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and a.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(assets)){
			if(Utils.isNotEmpty(assets.getName())){
				sql.append(" and instr(a.name,:name)>0 ");
				map.put("name", assets.getName());
			}
			if(Utils.isNotEmpty(assets.getType())){
				sql.append(" and instr(a.type,:type)>0 ");
				map.put("type", assets.getType());
			}
            if(Utils.isNotEmpty(assets.getSort())&&Utils.isNotEmpty(assets.getOrder())){
                sql.append(" order by a."+assets.getSort()+" "+assets.getOrder());
            } else {
                sql.append(" order by a.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Assets> list(Assets assets, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<Assets> rm = new BeanPropertyRowMapper<Assets>(Assets.class);
        StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.name");
        sql.append(",a.num,a.unit,a.startDate,a.price");
		sql.append(",a.newPrice,a.type,a.state,a.remark");
		sql.append(",a.create_time,a.create_user,a.update_time,a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from oa_assets a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id ");
        sql.append(" where a.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and a.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(assets)){
//			if(Utils.isNotEmpty(assets.getName())){
//				sql.append(" and instr(a.name,:name)>0 ");
//				map.put("name", assets.getName());
//			}
            if(Utils.isNotEmpty(assets.getSort())&&Utils.isNotEmpty(assets.getOrder())){
                sql.append(" order by a."+assets.getSort()+" "+assets.getOrder());
            } else {
                sql.append(" order by a.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public Assets getById(Assets assets) {
        RowMapper<Assets> rm = new BeanPropertyRowMapper<Assets>(Assets.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.name");
        sql.append(",a.num,a.unit,a.startDate,a.price");
		sql.append(",a.newPrice,a.type,a.state,a.remark");
		sql.append(",a.create_time,a.create_user,a.update_time,a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from oa_assets a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id ");
        sql.append(" where a.deleted=0 and sogzn.deleted=0 and a.id=:id ");
        map.put("id", assets.getId());
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(Assets assets, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into oa_assets (");
        sql.append("organization_id,name,num,unit");
		sql.append(",startDate,price,newPrice,type");
		sql.append(",state,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:name,:num,:unit");
		sql.append(",:startDate,:price,:newPrice,:type");
		sql.append(",:state,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("name", assets.getName());
        map.put("num", assets.getNum()==null?null:assets.getNum()+"");
        map.put("unit", assets.getUnit());
        map.put("startDate", assets.getStartdate());
        map.put("price", assets.getPrice());
        map.put("newPrice", assets.getNewprice());
        map.put("type", assets.getType());
        map.put("state", assets.getState());
        map.put("remark", assets.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Assets assets, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update oa_assets set ");
        sql.append("name=:name,num=:num,unit=:unit ");
		sql.append(",startDate=:startDate,price=:price,newPrice=:newPrice ");
		sql.append(",type=:type,state=:state,remark=:remark ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("name", assets.getName());
        map.put("num", assets.getNum()==null?null:assets.getNum()+"");
        map.put("unit", assets.getUnit());
        map.put("startDate", assets.getStartdate());
        map.put("price", assets.getPrice());
        map.put("newPrice", assets.getNewprice());
        map.put("type", assets.getType());
        map.put("state", assets.getState());
        map.put("remark", assets.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", assets.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(Assets assets, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(assets.getId().length()>0){
            for(String ID : assets.getId().split(",")){
                map  = new HashMap<String, String>();
                if(ID==null || ID.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update oa_assets set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", ID);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}